<?php
require_once('./extends/model/UserModel.class.php');
/**                                          
 * 用户资料配置
 * @author M2
 * @version 1.0
 * create Date 2013-04-27
 */
class UserProfileModel extends UserModel {
    /**
     * 获取用户资料信息	
     * @param
     */
    function getUserInfo($space = false){
        $userInfoList                      = $this->where('uid='.$this->uid)->field('id,uid,module,data,type')->select();
        $userInfo                          = $this->dataProcess( $userInfoList ,$space);
        $userInfo['detail']		           = $this->table(C('DB_PREFIX').'user')->where("uid={$this->uid}")->find();
        $userInfo['base']['completeness']  = 100;
        return $userInfo;
    }

	/**
	 * 数据处理	
	 * @param
	 */
    private function dataProcess( $userInfoList,$space ){
        $fieldList = $this->data_field(false,$space);
        foreach ($userInfoList as $value){
            if( $value['type'] == 'info' ){
                $database[ $value['module'] ] = unserialize( $value['data'] );
            }else{
                $data[ 'profile' ]['list'][] = array_merge( array('module'=>$value['module'],'id'=>$value['id']) , unserialize($value['data']) );
            }
        }
        $data['profile']['completeness'] = round( count( array_unique(common('Array')->getSubByKey( $data[ 'profile' ]['list'] ,'module') ) ) / 2 , 2) *100;
        foreach ($fieldList as $key=>$value){
            foreach ( $value as $k=>$v){
                $t = $database[$key][$k];
                if( $t ) $complete++;
                $data[$key]['list'][]  = array('field' => $k,'name'  => $v,'value' => $t );

            }
            $data[$key]['completeness'] = round( $complete/count($value) , 2 ) * 100 ;
            unset($complete);
        }
        unset($userInfoList);
        unset($fieldList);
        unset($database);
        return $data;
    }
}